﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RJ.Objects.GameEngine;
using System.Drawing;
using RJ.Objects.PokerCards;
using RJ.Euchre.App.Game;

namespace RJ.Euchre.App.Views
{
    public class MenuView : GameView
    {
        public event ContinueEventHandler Continue;

        private string backText = "Choose a back";
        private string continueText = "Continue ->";
        private SpriteSheet cardBack;
        private Background Background;
        private Rectangle[] cardBackSources;
        private Sprite[] backs;
        private Rectangle selectedBack;
        private Sprite continueSprite;
        private PointF gameCenter;
        private int backCount;
        private EuchreSettings euchreSettings;

        private Font font = new Font("Arial", (72 - (72 * 0.3f)));
        private Font fontc = new Font("Arial", ((72 - (72 * 0.3f)) / 2));
        private SizeF stringSize;
        private SizeF continueSize;

        public MenuView(GameState gameState)
            : this(gameState, new SpriteSheet(@"Resources\CardBacks_sm.png"), 4) { }

        public MenuView(GameState gameState, SpriteSheet cardBack, int backCount)
            : base(gameState)
        {
            this.cardBack = cardBack;
            this.backCount = backCount;
            euchreSettings = gameState.settings as EuchreSettings;
            var backImage = new Bitmap(GameState.GAME_WIDTH, GameState.GAME_HEIGHT);
            using (var backGraphics = Graphics.FromImage(backImage))
            {
                backGraphics.FillRectangle(new SolidBrush(Color.CadetBlue), new Rectangle(0, 0, backImage.Width, backImage.Height));
                stringSize = backGraphics.MeasureString(backText, fontc);
            }

            continueSprite = new Sprite(gameState, new SpriteSheet(@"Resources\ContinueBtn.png"),
                new Rectangle(0, 0, 150, 70), new RectangleF((backImage.Width - 175 - 10),
                    (backImage.Height - 90 - 10), 150, 70), PointF.Empty);

            Background = new Background(gameState, new Bitmap(@"Resources\Background.png"));
            gameCenter = new PointF(
                (GameState.GAME_WIDTH / 2),
                (GameState.GAME_HEIGHT / 2)
            );

            Initialize();
        }

        public override void ConsumeMouseEvent()
        {
            var queuedMouseEvent = gameState.GetMouseEvent();
            if (queuedMouseEvent != null)
            {
                // see if our mouse location lies within any of the card back points
                PointF loc = new PointF(queuedMouseEvent.X, queuedMouseEvent.Y);
                for (int x = 0; x < backs.Length; x++)
                {
                    var back = backs[x];

                    if (back.Destination.Contains(loc))
                    {
                        selectedBack = new Rectangle(
                            (int)(back.Destination.X - 2),
                            (int)(back.Destination.Y - 2),
                            (int)(back.Destination.Width + 4),
                            (int)(back.Destination.Height + 4)
                        );

                        if (gameState.settings is EuchreSettings)
                        {
                            (gameState.settings as EuchreSettings).CardBack = x + 1;
                        }
                        else
                        {
                            gameState.settings.AddSetting("cardback", x + 1);
                        }
                    }
                }

                if (continueSprite.Destination.Contains(queuedMouseEvent.Location))
                    OnContinue(new GameEventArgs(gameState));
            }
        }

        public override void ConsumeKeyboardEvent()
        {
            throw new NotImplementedException();
        }

        public override void Update(double gameTime, double elapsedTime)
        {
            ConsumeMouseEvent();
        }

        private Brush brushDOrange = new SolidBrush(Color.DarkOrange);

        public override void Draw(System.Drawing.Graphics g)
        {
            Background.Draw(g);
            if (selectedBack != null)
                g.DrawRectangle(new Pen(brushDOrange, 2f), selectedBack);

            g.DrawString(backText, fontc, brushDOrange, new PointF((gameCenter.X) - (stringSize.Width / 2), (gameCenter.Y - 136 - stringSize.Height)));
            continueSprite.Draw(g);

            base.Draw(g);
        }

        public override void Initialize()
        {
            GameObjects.Clear();
            cardBackSources = new Rectangle[backCount];
            backs = new Sprite[backCount];
            Size spriteSheetDimensions = cardBack.GetDimensions();

            int posX = 0;
            int posY = 0;
            int nextLength = spriteSheetDimensions.Width / backCount;
            Point[] quadrantModifiers = euchreSettings.QuadrantModifiers;
            Point[] distanceModifier = euchreSettings.DistanceModifier;

            for (int i = 0; i < backCount; i++)
            {
                Point modifier = quadrantModifiers[i];
                Point distMod = distanceModifier[i];
                Point fromCenter = new Point(10, 10);

                PointF dest = new PointF(
                    (gameCenter.X)  + ((fromCenter.X + (nextLength * distMod.X)) * modifier.X),
                    (gameCenter.Y) + ((fromCenter.Y + (spriteSheetDimensions.Height * distMod.Y)) * modifier.Y)
                );

                backs[i] = new Sprite(gameState, cardBack,
                    new Rectangle(
                        new Point(posX, posY),
                        new Size(nextLength, spriteSheetDimensions.Height)
                    ),
                    new RectangleF(
                        dest,
                        new SizeF(nextLength, spriteSheetDimensions.Height)
                    ),
                    new PointF(0f, 0f)
                );

                posX += nextLength;

                GameObjects.Add(backs[i]);
            }
        }

        protected void OnContinue(GameEventArgs e)
        {
            if (Continue != null)
                Continue(this, e);
        }
    }
}
